package com.yunupmall.uits;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class Filepicture {
    private static final String IMAGE_PATH = "D:\\JavaTomcat\\apache-tomcat-8.5.71\\webapps\\uploadimage\\";
    private static final String SERVER_PATH = "http://localhost:8080/uploadimage/";
    private static final String TEXTIMAGE_PATH = "D:\\JavaTomcat\\apache-tomcat-8.5.71\\webapps\\Textuploadimage\\";
    private static final String TEXTSERVER_PATH = "http://localhost:8080/Textuploadimage/";

    public static String upload(MultipartFile file) {
        //获取上传文件名
        String Filename = file.getOriginalFilename();
        //图片名字唯一性
        String replace = UUID.randomUUID().toString().replace("-", "");
        //将创建的名信息和旧文件名字进行拼接
        String newimageName = replace + "_" + Filename;
        File file1 = new File(IMAGE_PATH, newimageName);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        try {
            file.transferTo(file1);
        } catch (IOException e) {
            return null;
        }
        return SERVER_PATH + newimageName;
    }

    //文本编辑器图片上传
    public static String Textupload(MultipartFile file) {
        //获取上传文件名
        String Filename = file.getOriginalFilename();
        //图片名字唯一性
        String replace = UUID.randomUUID().toString().replace("-", "");
        //将创建的名信息和旧文件名字进行拼接
        String newimageName = replace + "_" + Filename;
        File file1 = new File(TEXTIMAGE_PATH, newimageName);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        try {
            file.transferTo(file1);
        } catch (IOException e) {
            return null;
        }
        return TEXTSERVER_PATH + newimageName;
    }
}
